package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderMapper;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: gali
 * @Date: 2022-07-22 15:54
 * @Description:
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;
    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("新建订单");
        orderMapper.create(order);
        log.info("订单微服务开始调用库存，做扣减操作");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("订单微服务开始调用库存，做扣减结束");
        log.info("订单微服务开始调用账户，做扣减操作");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("订单微服务开始调用账户，做扣减操作结束");
        log.info("修改订单状态");
        orderMapper.update(order.getUserId(),0);
        log.info("修改订单状态结束");
        log.info("下订单结束,O(∩_∩)O");
    }
}
